<template>
  <el-dialog :title="tatle" :visible="showDialog" @close="closeDialog" width="450px">
    <!-- 表单 -->
    <el-form label-width="80px" :model="form" :rules="rules" ref="refForm">
      <el-form-item label="所属学科" prop="subjectID" >
        <el-select v-model="form.subjectID" style="width:95%" @focus="simpleApi" placeholder="请选择"  >
        <el-option
        v-for="item in seleData"
        :key="item.value"
        :label="item.label"
        :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" prop="tagName">
        <el-input v-model="form.tagName" style="width:95%" placeholder="请输入标签名称" />
      </el-form-item>

    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addcon" >确定</el-button>
    </template>
  </el-dialog>
</template>

<script>

import { add, update, detail } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      tatle: '',
      seleData: [],
      form: {
        subjectID: '',
        tagName: '',
        id: null
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科目录名称', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    closeDialog () {
      this.$emit('update:showDialog', false)
      // 清空数据
      this.$refs.refForm.resetFields()
    },
    /**
     * 添加
     */
    addcon () {
      this.$refs.refForm.validate(async flag => {
        // console.log(flag)
        if (!flag) return
        if (this.form.id) {
          await update(this.form)
          // console.log(res)
          this.$message.success('修改成功！')
        } else {
          await add(this.form)
          this.$message.success('添加成功！')
        }

        this.$parent.$parent.apilist()
        this.closeDialog()
      })
    },
    async simpleApi () {
      const res = await simple({})
      this.seleData = res.data
      // console.log(res.data)
      // console.log(simple)
    },
    async tagsAdd (data) {
      const res = await detail(data)
      this.form = res.data
    }
  }
}

</script>

<style scoped lang='scss'>

</style>
